//
//  ReportListCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/12.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportListCell.h"

@implementation ReportListCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

//定义cell的宽度
- (void)setFrame:(CGRect)frame {
    frame.origin.x += 8;//8 为margin 值
    frame.size.width -= 2 * 8;
    [super setFrame:frame];
}


-(void)initView{
    _reportTitle = [[UILabel alloc]init];//报告标题
    _reportTitle.textColor = ColorFromRGB(0x333333);
    _reportTitle.font = [UIFont boldSystemFontOfSize:16.f];
    _delImageView =[[UIImageView alloc] init];
    _delImageView.exclusiveTouch = YES;//这个属性主要是解决多个控件同时响应事件的问题
    _delImageView.userInteractionEnabled = YES;//设置该视图可以交互和响应
    UITapGestureRecognizer *delImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickDelImageView:)];
    [_delImageView addGestureRecognizer:delImageTap];
    _delImage = [UIImage imageNamed:@"icon_delete"];
    _delImageView.image = _delImage;
    _line = [[UIView alloc]init];
    _line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    
    _reportIdTitle = [[UILabel alloc]init];   // 报告编号
    _reportIdTitle.text = @"报告编号:";
    _reportIdTitle.textColor = ColorFromRGB(0x333333);
    _reportIdTitle.font = [UIFont systemFontOfSize:14.f];
//    [_reportIdTitle setFrame:CGRectMake(12, 80, 70, 40)];
    
    _reportId = [[UILabel alloc]init];   // 报告编号
    _reportId.textColor = ColorFromRGB(0x666666);
    _reportId.font = [UIFont systemFontOfSize:14.f];
    
    _startDateTitle = [[UILabel alloc]init];  // 报表开始统计时间
    _startDateTitle.text = @"统计时间:";
    _startDateTitle.textColor = ColorFromRGB(0x333333);
    _startDateTitle.font = [UIFont systemFontOfSize:14.f];
    _startDate = [[UILabel alloc]init];  // 报表开始统计时间
    _startDate.textColor = ColorFromRGB(0x666666);
    _startDate.font = [UIFont systemFontOfSize:14.f];
    
    _addTimeTitle = [[UILabel alloc]init];// 报表生成时间
    _addTimeTitle.text = @"生成时间:";
    _addTimeTitle.textColor = ColorFromRGB(0x333333);
    _addTimeTitle.font = [UIFont systemFontOfSize:14.f];
    _addTime = [[UILabel alloc]init];// 报表生成时间
    _addTime.textColor = ColorFromRGB(0x666666);
    _addTime.font = [UIFont systemFontOfSize:14.f];
    
    _projectNumberTitle = [[UILabel alloc]init];// 统计项目个数
    _projectNumberTitle.text = @"项目个数:";
    _projectNumberTitle.textColor = ColorFromRGB(0x333333);
    _projectNumberTitle.font = [UIFont systemFontOfSize:14.f];
    _projectNumber = [[UILabel alloc]init];// 统计项目个数
    _projectNumber.textColor = ColorFromRGB(0x666666);
    _projectNumber.font = [UIFont systemFontOfSize:14.f];
    
    _deviceNumberTitle = [[UILabel alloc]init];// 统计设备个数(点位总数)
    _deviceNumberTitle.text = @"点位总数:";
    _deviceNumberTitle.textColor = ColorFromRGB(0x333333);
    _deviceNumberTitle.font = [UIFont systemFontOfSize:14.f];
    _deviceNumber = [[UILabel alloc]init];// 统计设备个数(点位总数)
    _deviceNumber.textColor = ColorFromRGB(0x666666);
    _deviceNumber.font = [UIFont systemFontOfSize:14.f];
    
    [self.contentView sd_addSubviews:@[_reportTitle,_delImageView,_line,_reportIdTitle,_reportId,
                                       _startDateTitle,_startDate,_addTimeTitle,_addTime,_projectNumberTitle,
                                       _projectNumber,_deviceNumberTitle,_deviceNumber]];
    
    _reportTitle.sd_layout.heightIs(40).leftSpaceToView(self.contentView, 12).rightSpaceToView(self.contentView, 50)
    .topSpaceToView(self.contentView, 0);
    _delImageView.sd_layout.heightIs(14).widthIs(14).rightSpaceToView(self.contentView, 28).topSpaceToView(self.contentView, 13);
    
    _line.sd_layout.heightIs(1).leftSpaceToView(self.contentView, 0).rightSpaceToView(self.contentView, 0)
    .topSpaceToView(_reportTitle, 0);
    
    _reportIdTitle.sd_layout.widthIs(70).heightIs(14)
    .leftSpaceToView(self.contentView, 12).topSpaceToView(_line, 12);
    
   
    
    _reportId.sd_layout.heightIs(14).widthIs((SCREEN_WIDTH-16)/2-88).leftSpaceToView(self.contentView, 84).topSpaceToView(_line, 12);

    _startDateTitle.sd_layout.heightIs(14).widthIs(70).leftSpaceToView(_reportId,6).topSpaceToView(_line, 12);
    _startDate.sd_layout.heightIs(14).widthIs((SCREEN_WIDTH-16)/2-88).leftSpaceToView(_startDateTitle, 0).topSpaceToView(_line, 12);

    _addTimeTitle.sd_layout.widthIs(70).heightIs(14).leftSpaceToView(self.contentView, 12).topSpaceToView(_startDateTitle, 12);
    _addTime.sd_layout.heightIs(14).leftSpaceToView(_addTimeTitle, 12).topSpaceToView(_startDateTitle, 12)
    .rightSpaceToView(self.contentView, 12);

    _projectNumberTitle.sd_layout.heightIs(14).widthIs(70).leftSpaceToView(self.contentView, 12).topSpaceToView(_addTimeTitle, 12);
    _projectNumber.sd_layout.heightIs(14).widthIs((SCREEN_WIDTH-16)/2-88).leftSpaceToView(_projectNumberTitle, 0)
    .topSpaceToView(_addTimeTitle, 12);

    _deviceNumberTitle.sd_layout.heightIs(14).widthIs(70).leftSpaceToView(_projectNumber,6).topSpaceToView(_addTimeTitle, 12);
    _deviceNumber.sd_layout.heightIs(14).widthIs((SCREEN_WIDTH-16)/2-88).leftSpaceToView(_deviceNumberTitle, 0)
    .topSpaceToView(_addTimeTitle, 12);
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self setupAutoHeightWithBottomView:_deviceNumberTitle bottomMargin:12];
    
    
}



-(void)setDataModel:(ReportListModel *)data rowIndex:(NSInteger)index{
    if(data != nil){
        _reportTitle.text = data.efairyreport_title;
        _reportId.text = data.efairyreport_id;
        _delImageView.tag = index;
        
        NSString *sTime;
        NSString *eTime;
        if(data.efairyreport_start_time && data.efairyreport_start_time.length >=10){
            sTime = [[data.efairyreport_start_time substringWithRange:NSMakeRange(5, 5)] stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
        }
        if(data.efairyreport_end_time && data.efairyreport_end_time.length >=10){
            eTime = [[data.efairyreport_end_time substringWithRange:NSMakeRange(5, 5)]stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
        }
        _startDate.text = [NSString stringWithFormat:@"%@-%@",sTime,eTime];
        _addTime.text = data.efairyreport_add_time;
        _projectNumber.text = data.efairyreport_project_number;
        _deviceNumber.text = data.efairyreport_device_number;
        
    }
}

-(void)onClickDelImageView:(id)tap{
    __weak typeof(self)weakSelf = self;
    if(weakSelf.delReportBlock){
        UITapGestureRecognizer *uitap = (UITapGestureRecognizer *)tap;
        UIImageView *view = (UIImageView*) uitap.view;
        weakSelf.delReportBlock(view.tag);
    }
}
@end
